'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class MessageFormattingService
    Inherits Core.Service
    Implements IMessageFormattingService

    Public Function FormatMessage(ByVal role As MessageSenderRole, ByVal message As IcqInterface.Interfaces.IMessage) As System.Windows.Documents.Paragraph Implements IMessageFormattingService.FormatMessage
        Dim messageContainer As Paragraph

        Dim prefix As Run
        Dim content As Run
        Dim suffix As Run

        messageContainer = New Paragraph
        messageContainer.Margin = New System.Windows.Thickness(0)

        prefix = New Run

        Select Case role
            Case MessageSenderRole.ContextIdentity
                prefix.Foreground = Brushes.Blue
            Case MessageSenderRole.FirstAtt
                prefix.Foreground = Brushes.Red
            Case MessageSenderRole.SecondAtt
                prefix.Foreground = Brushes.DarkGreen
            Case MessageSenderRole.ThirdAtt
                prefix.Foreground = Brushes.Orange
        End Select

        prefix.Text = String.Format("{0} ~ {1}: ", Date.Now.ToShortTimeString, message.Sender.Name)

        content = New Run

        content.Text = message.Text

        messageContainer.Inlines.Add(prefix)
        messageContainer.Inlines.Add(content)

        If TypeOf message Is IcqInterface.IcqOfflineMessage Then
            suffix = New Run

            suffix.Text = String.Format(" [Offline Message {0}]", DirectCast(message, IcqInterface.IcqOfflineMessage).OfflineSentDate)

            messageContainer.Inlines.Add(suffix)
        End If

        Return messageContainer
    End Function

    Public Function GetTextLinesFromDocument(ByVal document As System.Windows.Documents.FlowDocument) As String() Implements IMessageFormattingService.GetTextLinesFromDocument
        Dim lines As List(Of String)
        Dim writer As System.Text.StringBuilder

        lines = New List(Of String)

        For Each par As Paragraph In document.Blocks
            writer = New System.Text.StringBuilder

            For Each inline As Inline In par.Inlines
                Dim text As Run = TryCast(inline, Run)

                If text IsNot Nothing Then
                    writer.Append(text.Text)
                End If
            Next

            lines.Add(writer.ToString)
        Next

        Return lines.ToArray
    End Function

    Public Function FormatStatus(ByVal contact As ContactViewModel, ByVal status As IcqInterface.Interfaces.IStatusCode) As System.Windows.Documents.Paragraph Implements IMessageFormattingService.FormatStatus
        Dim statusContainer As Paragraph
        Dim content As Run

        statusContainer = New Paragraph
        statusContainer.Margin = New System.Windows.Thickness(0)

        content = New Run

        content.Foreground = System.Windows.Media.Brushes.Gray

        content.Text = String.Format("{0} ~ {1} changed his/her status to {2}", Date.Now.ToShortTimeString, contact.Name, status.DisplayName)

        statusContainer.Inlines.Add(content)

        Return statusContainer
    End Function
End Class